//
//  XJLHandelBuddyRequestViewController.m
//  XJL_WeChat
//
//  Created by XJL on 16/8/5.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLHandelBuddyRequestViewController.h"
#import "XJLContactHeaderView.h"

@interface XJLHandelBuddyRequestViewController ()

/** 通讯录头部*/
@property(nonatomic,strong) XJLContactHeaderView *contactHeadView;
/** 好友请求*/
@property(nonatomic,strong) NSMutableArray *buddyRequestsM;
@end

@implementation XJLHandelBuddyRequestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.buddyRequestsM = [NSMutableArray array];
    //1.加载偏好设置的数据
    NSUserDefaults *defautls = [NSUserDefaults standardUserDefaults];
    NSArray *buddyRequest = [defautls objectForKey:@"BuddyRequest"];
    [self.buddyRequestsM addObjectsFromArray:buddyRequest];
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.buddyRequestsM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"buddyRequestM";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        //添加同意按钮
        UIButton *acceptBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [acceptBtn setTitle:@"同意" forState:UIControlStateNormal];
        acceptBtn.backgroundColor = [UIColor grayColor];
        [acceptBtn addTarget:self action:@selector(acceptBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [acceptBtn sizeToFit];
        cell.accessoryView = acceptBtn;
        
        
        //添加拒绝按钮
//        UIButton *regectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [regectBtn setTitle:@"拒绝" forState:UIControlStateNormal];
//        regectBtn.backgroundColor = [UIColor grayColor];
//        [regectBtn addTarget:self action:@selector(regectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//        [regectBtn sizeToFit];
//        cell.accessoryView = regectBtn;
        
        
        
    }
    UIView *acceptBtn = cell.accessoryView;
    acceptBtn.tag = indexPath.row;
    
    //取出对象行的数据
    NSDictionary *buddyRequest = self.buddyRequestsM[indexPath.row];
    
    //显示用户名
    cell.textLabel.text = buddyRequest[@"username"];
    cell.detailTextLabel.text = buddyRequest[@"message"];
    
    return cell;
}

-(void)acceptBtnClick:(UIButton *)btn{
    //同意请求
    //1.取出对应行的数据
    NSDictionary *buddyRequest = self.buddyRequestsM[btn.tag];
    //用户名
    NSString *username = buddyRequest[@"username"];
    [[EaseMob sharedInstance].chatManager acceptBuddyRequest:username error:nil];
    //
    self.contactHeadView.redView.hidden = buddyRequest.count == 0;
    [self reloadData:username];
}

-(void)regectBtnClick:(UIButton *)btn{
    //同意请求
    //1.取出对应行的数据
    NSDictionary *buddyRequest = self.buddyRequestsM[btn.tag];
    //用户名
    NSString *username = buddyRequest[@"username"];
    [[EaseMob sharedInstance].chatManager rejectBuddyRequest:username reason:@"No Why,No Reason" error:nil];
    
    [self reloadData:username];
}

-(void)reloadData:(NSString *)username{
    for (NSDictionary *dict in self.buddyRequestsM) {
        if ([username isEqualToString:dict[@"username"]]) {
            [self.buddyRequestsM removeObject:dict];
            break;
        }
    }
    
    //重新保存到沙盒
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.buddyRequestsM forKey:@"BuddyRequest"];
    [defaults synchronize];
    
    [self.tableView reloadData];
}

#pragma mark 实现此方法,就会出现"删除" 按钮
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //取出对应行数据
    NSDictionary *buddyRequest = self.buddyRequestsM[indexPath.row];
    
    NSString *username = buddyRequest[@"username"];
    
    [[EaseMob sharedInstance].chatManager rejectBuddyRequest:username reason:@"No Why,No Reason" error:nil];
    
    [self reloadData:username];
}

@end
